package org.codegallery.javagal.xml.xpath;

import org.codegallery.javagal.Constants;
import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: gongwenwei
 * Date: 11-10-17
 * Time: 上午10:43
 * To change this template use File | Settings | File Templates.
 */
public class SampXPath {
    public static void main(String[] args){
        SampXPath sa=new SampXPath();
        try{
            sa.evalXPathValue();
        }catch(Exception ex){
            ex.printStackTrace();
        }

    }

    public void evalXPathValue() throws Exception{
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = null;
        db = dbf.newDocumentBuilder();
        Document doc = db.parse(new File(Constants.TEST_RES_DIR+"\\xsltsample\\sample1.xml"));
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        System.out.println(xPath.evaluate("//catalog/cd/title/text()", doc, XPathConstants.NODE));
    }

}
